package com.myself.common.define.util;

import cn.hutool.json.JSONObject;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.myself.common.define.config.JwtConfig;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;

@Data
@NoArgsConstructor
public class CustomJwtUtil {

    public static JwtConfig jwtConfigProperties;


    /**
     * 生成认证令牌
     *
     * @param userName 用户登录名
     * @return 认证令牌
     */
    public static String generateAccessToken(String userName) {
        JwtConfig.AccessTokenProperties accessToken = jwtConfigProperties.getAccessToken();
        LinkedHashMap<String, Object> payloadMap = new LinkedHashMap<>() {{
            put("userName", userName);
            put("exp", LocalDateTime.now().plusHours(accessToken.getExpiryHours()));
        }};
        return JWTUtil.createToken(payloadMap, accessToken.getSecurityKey().getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 生成认证令牌
     *
     * @param payloadMap 用户信息载体
     * @return 认证令牌
     */
    public static String generateAccessToken(Map payloadMap) {
        JwtConfig.AccessTokenProperties accessToken = jwtConfigProperties.getAccessToken();
        payloadMap.put("exp", LocalDateTime.now().plusHours(accessToken.getExpiryHours()));
        return JWTUtil.createToken(payloadMap, accessToken.getSecurityKey().getBytes(StandardCharsets.UTF_8));
    }


    /**
     * 生成 刷新令牌
     *
     * @param userName 用户登录名
     * @return 刷新令牌
     */
    public static String generateRefreshToken(String userName) {
        JwtConfig.RefreshTokenProperties refreshToken = jwtConfigProperties.getRefreshToken();
        LinkedHashMap<String, Object> payloadMap = new LinkedHashMap<>() {{
            put("userName", userName);
            put("exp", LocalDateTime.now().plusHours(refreshToken.getExpiryHours()));
        }};

        return JWTUtil.createToken(payloadMap, refreshToken.getSecurityKey().getBytes(StandardCharsets.UTF_8));
    }


    /**
     * 校验 认证令牌
     *
     * @param accessToken 认证令牌
     * @return
     */
    public static Boolean verifyAccessToken(String accessToken) {
        JwtConfig.AccessTokenProperties properties = jwtConfigProperties.getAccessToken();
        boolean verify = JWTUtil.verify(accessToken, properties.getSecurityKey().getBytes(StandardCharsets.UTF_8));

        return verify;
    }

    /**
     * 校验 刷新令牌
     *
     * @param refreshToken 刷新令牌
     * @return
     */
    public static Boolean verifyRefreshToken(String refreshToken) {
        JwtConfig.RefreshTokenProperties properties = jwtConfigProperties.getRefreshToken();
        boolean verify = JWTUtil.verify(refreshToken, properties.getSecurityKey().getBytes(StandardCharsets.UTF_8));
        return verify;
    }

    /**
     * 解析 令牌
     *
     * @param token 令牌
     * @return
     */
    public static JSONObject parseToken(String token) {
        JWT jwt = JWTUtil.parseToken(token);
        return jwt.getPayloads();
    }


}
